package view;

import my_utils.MyTools;
import service.StuService;
import service.TeaService;
import user.Student;
import user.Teacher;

import java.util.Scanner;

public class StuManageView {
    Scanner scanner = new Scanner(System.in);
    boolean key = true;
    String input = "";
    private StuService stuService = new StuService();
    private TeaService teaService = new TeaService();

    //显示主菜单
    public void MainJudge() {
        do {
            System.out.println("**********************学生信息管理系统*******************");
            System.out.println("\t\t\t0 退出");
            System.out.println("\t\t\t1 教师注册");
            System.out.println("\t\t\t2 教师登录");

            System.out.print("请选择：");
            input = scanner.next();
            switch (input) {
                case "0": {
                    key = MyTools.exit();
                    break;
                }
                case "1": {
                    TeaSignUp();//教师注册
                    break;
                }
                case "2": {
                    if (TeaLogIn()) {//教师登录
                        StuInfoManage();//学生信息管理
                    }
                    break;
                }
                default: {
                    System.out.println("选择错误（只能选择0-2），重新选择");
                    break;
                }
            }
        } while (key);
        System.out.println("已退出学生信息管理系统");
    }

    //教师注册
    public void TeaSignUp() {
        //name, sex, age, id, password, phoneNum
        System.out.println("-------------------------教师注册----------------------");
        System.out.println("请填写下面的注册信息：");
        System.out.print("姓名：");
        String name = MyTools.checkName();
        System.out.print("性别：");
        char sex = MyTools.checkGender();
        System.out.print("年龄：");
        int age = MyTools.checkAge();
        System.out.print("学工号：");
        String id = MyTools.checkId();
        System.out.print("密码：");
        String password = MyTools.checkPassword();
        System.out.print("手机号：");
        String phoneNum = MyTools.checkPhoneNum();
        Teacher teacher = new Teacher(name, sex, age, id, password, phoneNum);
        if (teaService.ThSignUp(teacher)) {
            System.out.println("教师注册成功");
        }
    }

    //教师登录
    public boolean TeaLogIn() {
        System.out.println("---------------------教师登录界面-------------------");
        do {
            System.out.println("请输入登录信息：");
            System.out.print("学工号：");
            String id = MyTools.checkId();
            System.out.print("密码：");
            String password = MyTools.checkPassword();
            if (teaService.ThLogIn(id, password)) {
                System.out.println("登录成功");
                return true;
            } else {
                System.out.println("学工号或密码错误，是否继续登录(y/n)");
                char choice = scanner.next().charAt(0);
                if (choice == 'n' || choice == 'N') {
                    break;
                }
            }
        } while (true);

        return false;
    }

    //显示对学生信息的操作
    public void StuInfoManage() {
        boolean loop = true;
        do {
            System.out.println("----------------------学生信息管理------------------");
            System.out.println("\t\t\t0. 退出");
            System.out.println("\t\t\t1. 添加学生信息");
            System.out.println("\t\t\t2. 删除学生信息");
            System.out.println("\t\t\t3. 显示学生列表");
            System.out.println("\t\t\t4. 修改学生信息");
            System.out.println("\t\t\t5. 查看学生信息");
            System.out.print("请输入你的选择：");
            int choose = scanner.nextInt();

            switch (choose) {
                case 0: {
                    loop = MyTools.exit();
                    break;
                }
                case 1: {
                    addStudent();
                    break;
                }
                case 2: {
                    delStudent();
                    break;
                }
                case 3: {
                    listStudent();
                    break;
                }
                case 4: {
                    ModStu();
                    break;
                }
                case 5: {
                    FindStu();
                    break;
                }
                default: {
                    System.out.println("你的输入(0-4)有误，请重新输入");
                }
            }
        } while (loop);
    }

    //编写listStudent()显示学生列表
    public void listStudent() {
        System.out.println("-------------------------------------------学生列表-------------------------------------");
        System.out.println("序号\t\t姓名\t\t\t性别\t\t年龄\t\t学号\t\t\t密码\t\t\t手机号\t\t\t班级\t\t成绩");
        Student[] students = stuService.list();//得到所有学生的信息
        for (int i = 0; i < students.length; i++) {
            if (students[i] == null) {
                break;//如果为null，就不显示学生信息了
            }
            System.out.println(students[i]);
        }
        System.out.println("---------------------------------------学生列表显示完毕-----------------------------------");
    }

    //编写addStudent()接收Student对象，调用add方法
    public void addStudent() {
        System.out.println("----------------------------添加学生------------------------");
        System.out.print("姓名：");
        String name = MyTools.checkName();
        System.out.print("性别：");
        char sex = MyTools.checkGender();
        System.out.print("年龄：");
        int age = MyTools.checkAge();
        System.out.print("学号：");
        String id = MyTools.checkId();
        System.out.print("密码：");
        String password = MyTools.checkPassword();
        System.out.print("手机号：");
        String phoneNum = MyTools.checkPhoneNum();
        System.out.print("班级：");
        String class_ = MyTools.checkClass();
        System.out.print("成绩：");
        double score = MyTools.checkScore();
        //创建一个新的Student对象
        Student newStudent = new Student(name, sex, age, id, password, phoneNum, class_, score);
        if (stuService.add(newStudent)) {
            System.out.println("添加学生成功");
        } else {
            System.out.println("添加学生失败，该学生已经注册过了");
        }
    }

    //编写删除学生信息的界面，接收输入的编号，调用Service的del方法
    public void delStudent() {
        System.out.println("---------------------------删除学生信息-------------------------");
        System.out.print("请输入待删除学生的编号（0退出）：");
        int delNum = MyTools.checkNum();
        if (delNum == 0) {
            System.out.println("----------------放弃删除学生信息------------------");
            return;//结束本方法
        }
        //System.out.print("请确认是否删除（Y/N），请小心选择：");
        char choice = MyTools.confirmDel();//该方法本身就有循环判断的逻辑
        if (choice == 'Y') {//调用放弃删除的方法
            if (stuService.del(delNum)) {
                System.out.println("-------------------删除成功-------------------");
            } else {
                System.out.println("----------------学生编号不存在，删除失败------------");
            }
        } else {
            System.out.println("----------------放弃删除学生信息------------------");
        }
    }

    //查看学生信息
    public void FindStu() {
        System.out.println("-----------------------查找学生----------------------");
        System.out.println("请选择要查找的学生的序号：");
        int searchNum = MyTools.checkNum();
        Student stu = stuService.FindStu(searchNum);
        if (stu != null) {
            System.out.println(stu);
        } else {
            System.out.println("未查找到学生信息");
        }
    }

    //修改学生信息,通过调用FindStu看有没有该学生信息，有就通过set方法修改
    public void ModStu() {
        System.out.println("---------------------修改学生信息----------------------");
        System.out.print("请选择待修改的学生序号（0退出）：");
        int modNum = MyTools.checkNum();
        if (modNum == 0) {
            System.out.println("--------------------已放弃修改学生信息----------------");
            return;
        }
        Student stu = stuService.FindStu(modNum);//返回的是引用类型
        if (stu == null) {
            System.out.println("----------------要修改信息的学生编号不存在------------");
            return;
        }
        System.out.print("姓名（" + stu.getName() + ")：");
        String name = MyTools.checkName();
        stu.setName(name);
        System.out.print("性别（" + stu.getSex() + ")：");
        char gender = MyTools.checkGender();
        stu.setSex(gender);
        System.out.print("年龄（" + stu.getAge() + ")：");
        int age = MyTools.checkAge();
        stu.setAge(age);
        System.out.print("学号（" + stu.getId() + ")：");
        String id = MyTools.checkId();
        stu.setId(id);
        System.out.print("密码（" + stu.getPassword() + ")：");
        String password = MyTools.checkPassword();
        stu.setPassword(password);
        System.out.print("手机号（" + stu.getPhoneNum() + ")：");
        String phoneNum = MyTools.checkPhoneNum();
        stu.setPhoneNum(phoneNum);
        System.out.print("班级（" + stu.getClass_() + ")：");
        String class_ = MyTools.checkClass();
        stu.setClass_(class_);
        System.out.print("成绩（" + stu.getScore() + ")：");
        double score = MyTools.checkScore();
        stu.setScore(score);
    }
}
